import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/cupertino.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/util/show_policy_dialog.dart';
import 'package:permission_handler/permission_handler.dart';


class PermissionCenterConfig {
  static Map<Permission,dynamic> permissionMap = {
    Permission.camera:{
      "title": "相机权限",
      "msg": "用于扫描二维码、照片拍摄上传",
      "permission": Permission.camera,
      "hasSet": false,
      "key": "cameraPermission"
    },
    Permission.photos:{
      "title": "相册权限",
      "msg": "用于照片上传",
      "permission": Permission.photos,
      "hasSet": false,
      "key": "photoPermission"
    },
    Permission.storage:{
      "title": "相册权限",
      "msg": "用于照片上传",
      "permission": Permission.storage,
      "hasSet": false,
      "key": "storagePermission"
    },
    Permission.bluetoothScan:{
      "title": "蓝牙权限",
      "msg": "用于连接蓝牙健康设备",
      "permission": Permission.bluetoothScan,
      "hasSet": false,
      "key": "bluetoothPermission"
    },
    Permission.bluetoothConnect:{
      "title": "蓝牙权限",
      "msg": "用于连接蓝牙健康设备",
      "permission": Permission.bluetoothConnect,
      "hasSet": false,
      "key": "bluetoothPermission"
    },
    Permission.notification:{
      "title": "通知权限",
      "msg": "用于通知、消息接收",
      "permission": Permission.notification,
      "hasSet": false,
      "key": "notificationPermission"
    },
    Permission.location:{
      "title": "定位权限",
      "msg": "工单执行、蓝牙扫描需要获取定位权限",
      "permission": Permission.location,
      "hasSet": false,
      "key": "locationPermission"
    }

    // Permission.locationWhenInUse:{
    //   "title": "定位权限",
    //   "msg": "蓝牙扫描需要获取定位权限",
    //   "permission": Permission.locationWhenInUse,
    //   "hasSet": false,
    //   "key": "locationPermission"
    // }
  };

  static Future<void> initPermissionConfig() async {
    for(var item in permissionMap.entries){
      String key = item.value['key'];
      var hasSet =  SpUtil.getBool(key,defValue: false);
      item.value['hasSet'] = hasSet;
      var status = await item.key.status;
      if(status==PermissionStatus.granted||status==PermissionStatus.limited){
        item.value['setName'] = "已开启";
      }else{
        item.value['setName'] = "去设置";
      }
    }
  }

  static Future<void> savePermissionInit(Permission permission) async {
    var item = permissionMap[permission];
    item['hasSet'] = true;
    String key = item['key'];
    await SpUtil.putBool(key, true);
  }
}

extension PermissionExt on Permission{

  Future<bool> hasPermission() async {
    var status = await this.status;
    return status == PermissionStatus.granted;
  }

  Future<void> checkRequestPermission(BuildContext context,{required Function() onSuccess,required String msg,bool showAgain = true}) async {
    var permissionInfo = PermissionCenterConfig.permissionMap[this];
    var status = await this.status;
    if(status!=PermissionStatus.granted&&status!=PermissionStatus.limited){
      if(permissionInfo['hasSet']==true){
        if(showAgain){
          showApplyDialog(context: context, onSubmit: () async {
            await openAppSettings();
          },message: '${msg}',applyText: '去设置');
        }
      }else{
        ///请求权限
        showPermissionDialog(context, msg: "${msg}", onApplyListener: () async {
          var result = await this.request();
          if(result==PermissionStatus.granted||result==PermissionStatus.limited){
            onSuccess.call();
          }else{
            showApplyDialog(context: context, onSubmit: (){},message: '后面您可在设置-系统权限管理中开启');
          }
          PermissionCenterConfig.savePermissionInit(this);
        });
      }
    }else{
      onSuccess.call();
    }
  }
}
